package com.company.zoj;

import java.util.LinkedList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Scanner;

/**
 * Created by v on 17-6-5.
 */
public class ZojTest1406 {
    private static List<List<Edge>> lists;
    private static boolean[] isVisited;

    private static class Edge implements Comparable<Edge>{
        int target;
        int weight;

        public Edge(int target,int weight){
            this.target=target;
            this.weight=weight;
        }

        @Override
        public int compareTo(Edge o) {
            if(o.weight==weight){
                return 0;
            }else if(weight<o.weight){
                return -1;
            }else {
                return 1;
            }
        }
    }

    private static int solve(int point){
        int sum=0;
        int current=1;
        isVisited=new boolean[point];
        PriorityQueue<Edge> queue=new PriorityQueue<>();

        isVisited[0]=true;
        for(Edge edge:lists.get(0)){
            queue.offer(edge);
        }

        while (current<point){
            Edge edge=queue.poll();
            if(isVisited[edge.target]){
                continue;
            }

            sum+=edge.weight;
            isVisited[edge.target]=true;
            for(Edge ed:lists.get(edge.target)){
                queue.offer(ed);
            }

            current++;
        }

        return sum;
    }


    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        while (true){
            int point=input.nextInt();
            if(point==0){
                return;
            }

            lists=new LinkedList<>();
            for(int i=0;i<point;i++){
                lists.add(new LinkedList<Edge>());
            }
            for(int i=0;i<point-1;i++){
                int begin=input.next().charAt(0)-'A';
                int n=input.nextInt();
                for(int j=0;j<n;j++){
                    int end=input.next().charAt(0)-'A';
                    int weight=input.nextInt();
                    lists.get(begin).add(new Edge(end,weight));
                    lists.get(end).add(new Edge(begin,weight));
                }
            }

            System.out.println(solve(point));
        }
    }
}
